package com.keke.myblogv1.usermessage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keke.myblogv1.girlfriend.entity.MyBlogGirlfriend;
import com.keke.myblogv1.girlfriend.mapper.MyBlogGirlfriendMapper;
import com.keke.myblogv1.usermessage.dto.QueryUserMessageDTO;
import com.keke.myblogv1.usermessage.entity.MyBlogUserMessage;
import com.keke.myblogv1.usermessage.mapper.MyBlogUserMessageMapper;
import com.keke.myblogv1.usermessage.service.MyBlogUserMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Derrick
 * @since 2022-07-31
 */
@Service
public class MyBlogUserMessageServiceImpl extends ServiceImpl<MyBlogUserMessageMapper, MyBlogUserMessage> implements MyBlogUserMessageService {

    @Resource
    private MyBlogUserMessageMapper myBlogUserMessageMapper;

    @Override
    public List<MyBlogUserMessage> getMyBlogUserMessageList() {
        LambdaQueryWrapper<MyBlogUserMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MyBlogUserMessage::getStatus,"1");
        List<MyBlogUserMessage> myBlogUserMessages = myBlogUserMessageMapper.selectList(queryWrapper);
        return myBlogUserMessages;
    }

    @Override
    public Page<MyBlogUserMessage> getMyBlogUserMessageListByPage(QueryUserMessageDTO queryUserMessageDTO) {
        Page<MyBlogUserMessage> page = new Page<>(queryUserMessageDTO.getPageNum(), queryUserMessageDTO.getPageSize());
        LambdaQueryWrapper<MyBlogUserMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MyBlogUserMessage::getStatus,"1");
        queryWrapper.orderByDesc(MyBlogUserMessage::getDate);
        Page<MyBlogUserMessage> myBlogUserMessagePage = myBlogUserMessageMapper.selectPage(page, queryWrapper);
        return myBlogUserMessagePage;
    }

    @Override
    public boolean updateMessage(MyBlogUserMessage myBlogUserMessage) {
        myBlogUserMessage.setUpdateBy("admin");
        myBlogUserMessage.setUpdateDate(new Date());
        int i = myBlogUserMessageMapper.updateById(myBlogUserMessage);
        if(i>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean insertMessage(MyBlogUserMessage myBlogUserMessage) {
        int insert = myBlogUserMessageMapper.insert(myBlogUserMessage);
        if(insert>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean deleteMessage(MyBlogUserMessage myBlogUserMessage) {
        myBlogUserMessage.setStatus("0");
        myBlogUserMessage.setUpdateBy("admin");
        myBlogUserMessage.setUpdateDate(new Date());
        int i = myBlogUserMessageMapper.updateById(myBlogUserMessage);
        if(i>0){
            return true;
        }else{
            return false;
        }
    }
}
